package com.difsonapps.android.joinme.db;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseViewMapHistory  extends SQLiteOpenHelper{
	
	
	// All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
	private final String TAG = this.getClass().getSimpleName();
    // Database Name
    private static final String DATABASE_NAME = "DataBaseViewMapHistory";
 
    // Contacts table name
    private static final String TABLE_HISTORY = "ViewMapHistory";
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_CN_ID = "contact_id";
    public static final String KEY_DATE = "date";

    
	public DataBaseViewMapHistory(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_HISTORY + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
                + KEY_CN_ID + " BIGINT,"
                + KEY_DATE  + " DATETIME" 
        		+ ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
		
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
 
        // Create tables again
        onCreate(db);
		
	}
	
    // Adding new permission
    public long addContactViewMap(ViewMapHistoryTable data) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_CN_ID, data.getContactId()); // Contact ID
        values.put(KEY_DATE, dateFormat.format(data.getDate())); // Date
  
  

        // Inserting Row
        long viewmap_id = db.insert(TABLE_HISTORY, null, values);
        db.close(); // Closing database connection
        return viewmap_id;
    }
	
    // Getting single ViewMapHistory
    public ViewMapHistoryTable getContactViewMap(int id) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
        SQLiteDatabase db = this.getReadableDatabase();
        ViewMapHistoryTable data = null;
        Cursor cursor = db.query(TABLE_HISTORY, new String[] { KEY_ID,KEY_CN_ID,
        		KEY_DATE}, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null,null,null);
        if (cursor != null && cursor.moveToFirst())
        { 
        	try {
				data = new ViewMapHistoryTable(Integer.parseInt(cursor.getString(0)),Long.parseLong(cursor.getString(1)),
						dateFormat.parse(cursor.getString(2)));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
           
        // return contact
        return data;
    }
    
    public List<ViewMapHistoryTable> getAllHistory() {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
        List<ViewMapHistoryTable> historyList = new ArrayList<ViewMapHistoryTable>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_HISTORY;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ViewMapHistoryTable data = new ViewMapHistoryTable() ;
                data.setId(Integer.parseInt(cursor.getString(0)));
                data.setContactId(Long.parseLong(cursor.getString(1)));
                try {
					data.setDate(dateFormat.parse(cursor.getString(2)));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

                // Adding contact to list
                historyList.add(data);
            } while (cursor.moveToNext());
        }
 
        // return contact list
        return historyList;
    }
    
    
    // Getting All Contacts
    public HashMap<String,ViewMapHistoryTable> getMapHistory() {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
        HashMap<String,ViewMapHistoryTable> historyMap = new HashMap<String,ViewMapHistoryTable>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_HISTORY;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ViewMapHistoryTable data = new ViewMapHistoryTable() ;
                data.setId(Integer.parseInt(cursor.getString(0)));
                data.setContactId(Long.parseLong(cursor.getString(1)));
                try {
					data.setDate(dateFormat.parse(cursor.getString(2)));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

                // Adding contact to list
                historyMap.put(cursor.getString(0),data);
            } while (cursor.moveToNext());
        }
 
        // return contact list
        return historyMap;
    }
	

}
